package com.example.myapplication;

import com.example.baselib.base.BaseAbility;
import com.example.baselib.utils.PermissionUtils;
import com.example.baselib.utils.log.LogUtils;
import com.example.myapplication.slice.MainAbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.window.dialog.ToastDialog;
import ohos.bundle.IBundleManager;

public class MainAbility extends BaseAbility {

    public static final String ACTION = "example.action.main";


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(MainAbilitySlice.class.getName());
        String[] ps = {"ohos.permission.GET_NETWORK_INFO",
                "ohos.permission.WRITE_MEDIA",
                "ohos.permission.CAMERA"};

        if (PermissionUtils.verifyPermission(this, ps) != null) {
            requestPermissionsFromUser(ps, 101);
            if (PermissionUtils.prohibitPermission(this, ps) != null) {
                LogUtils.d("已经拒绝,请手动开启");
            }
        }

    }

    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 101:
                if (grantResults.length > 0 && grantResults[0] == IBundleManager.PERMISSION_GRANTED) {
                    LogUtils.i("储存权限授权成功 ");
                } else {
                    // todo 权限被拒绝
                    new ToastDialog(this)
                            .setDuration(2500)
                            .setText("拒绝授权部分功能将无法使用,请手动开启")
                            .show();
                }
                return;
            default:
                break;
        }
    }

}
